home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / fsinfo / fsinfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-21  |  9.6 KB  |  397 lines

  1. /*
  2.  * fsinfo.c --
  3.  *
  4.  *     Print out information related to the file systems, if any, found
  5.  *  on the disk devices (/dev/rsd*) passed as arguments.
  6.  */
  7.  
  8. #ifndef lint
  9. static char rcsid[] = "$Header: /sprite/src/cmds/fsinfo/RCS/fsinfo.c,v 1.5 91/10/28 14:38:48 voelker Exp Locker: voelker $ SPRITE (Berkeley)";
  10. #endif not lint
  11.  
  12. #include "sprite.h"
  13. #include "sys/file.h"
  14. #include "stdio.h"
  15. #include "errno.h"
  16.  
  17. #include "disk.h"
  18. #include "option.h"
  19.  
  20. #define GENERIC_OFS_PREFIX  "(new domain)"
  21.  
  22. /*
  23.  * output strings
  24.  */
  25. #define LFS_NAME            "lfs"
  26. #define OFS_NAME            "ofs"
  27. #define EMPTY_PREFIX_NAME   "(none)"
  28.  
  29. /*
  30.  * delayed printing of header;  only print the header if
  31.  * more information is going to follow it
  32.  */
  33. static Boolean headerNotPrinted;
  34.  
  35. /*
  36.  * If true, fsinfo will print out error messages.  fsinfo will
  37.  * complain if it finds a file system on a partition on which
  38.  * the file system was not created;  if it tries to open an invalid
  39.  * partition, which includes the device for the entire disk
  40.  * (e.g., /dev/rsd00);  or, if no file system is found, a message
  41.  * to that effect
  42.  */
  43. Boolean verbose = FALSE;
  44.  
  45. Option optionArray[] = {
  46.     {OPT_DOC, "", (Address)NIL,
  47.      "fsinfo [-verbose] device [device]..."},
  48.     {OPT_TRUE, "verbose", (Address)&verbose,
  49.      "Print error messages"}
  50. };
  51. static int numOptions = sizeof(optionArray) / sizeof(Option);
  52.  
  53.  
  54. /*
  55.  *----------------------------------------------------------------------
  56.  *
  57.  * PartitionIndex --
  58.  *
  59.  *    Calculates the partition number/index using the device name,
  60.  *      e.g., /dev/rsd00a would have a partition index of 0.
  61.  *      Partition designations start at 'a' and are limited by the
  62.  *      number of partitions on the disk, as specified in the disk label.
  63.  *
  64.  * Results:
  65.  *    If no valid partition index could be found from the device
  66.  *      name, then an index of -1 is returned;  otherwise, the integer
  67.  *      index corresponding to the partition is returned.
  68.  *
  69.  * Side effects:
  70.  *    None.
  71.  *
  72.  *----------------------------------------------------------------------
  73.  */
  74. int
  75. PartitionIndex(deviceName, labelPtr)
  76.     char *deviceName;
  77.     Disk_Label *labelPtr;
  78. {
  79.     if (deviceName != NULL) {
  80.     char c;
  81.     int n;
  82.  
  83.     c = deviceName[strlen(deviceName) - 1];
  84.     n = (int)(c - 'a');
  85.     if (n < 0 || n > labelPtr->numPartitions - 1) {
  86.         return -1;
  87.     }
  88.     return n;
  89.     }
  90.     return -1;
  91. }
  92.  
  93. /*
  94.  *----------------------------------------------------------------------
  95.  *
  96.  * PrintHeader --
  97.  *
  98.  *      Prints the column labels to stdout.
  99.  *
  100.  * Results:
  101.  *      None.
  102.  *
  103.  * Side effects:
  104.  *    Prints to stdout.
  105.  *
  106.  *----------------------------------------------------------------------
  107.  */
  108. void
  109. PrintHeader()
  110. {
  111.     printf("%-13s %-3s  %-15s  %8s  %8s  %9s  %7s\n",
  112.        "Dev Name", "FS", "Prefix", "Dom. Num", "SpriteID",
  113.        "Start Cyl", "End Cyl");
  114. }       
  115.  
  116. /*
  117.  *----------------------------------------------------------------------
  118.  *
  119.  * PrintInfo --
  120.  *
  121.  *      Prints the file system type, the domain prefix, the domain number,
  122.  *      the SpriteID, the start cylinder and size, and the index of
  123.  *      the partition denoted by `deviceName'.
  124.  *
  125.  * Results:
  126.  *      None.
  127.  *
  128.  * Side effects:
  129.  *    Prints to stdout.
  130.  *
  131.  *----------------------------------------------------------------------
  132.  */
  133. void
  134. PrintInfo(deviceName, fsType, domainPrefix, domainNumber, spriteID, 
  135.       labelPtr, index)
  136.     char       *deviceName;             /* partition name, e.g., /dev/rsd00a */
  137.     char       *fsType;                 /* file system type */
  138.     char       *domainPrefix;           /* domain prefix */
  139.     int        domainNumber, spriteID;  /* domain # and Sprite ID */
  140.     Disk_Label *labelPtr;               /* disk label */
  141.     int        index;                   /* partition index */
  142. {
  143.     int startCyl, endCyl;
  144.     char *prefix;
  145.  
  146.     if (labelPtr == NULL) {
  147.     return;
  148.     }
  149.     startCyl = labelPtr->partitions[index].firstCylinder;
  150.     endCyl = labelPtr->partitions[index].numCylinders;
  151.     if (endCyl > 0) {
  152.     endCyl += startCyl - 1;
  153.     } else {
  154.     endCyl = startCyl;
  155.     }
  156.     if (domainPrefix == NULL || *domainPrefix == NULL) {
  157.     prefix = EMPTY_PREFIX_NAME;
  158.     } else if (!strcmp(GENERIC_OFS_PREFIX, domainPrefix)) {
  159.     prefix = EMPTY_PREFIX_NAME;
  160.     } else {
  161.     prefix = domainPrefix;
  162.     } 
  163.     printf("%-13s %3s  %-15s  %8d  %8d  %9d  %7d\n",
  164.        deviceName, fsType, prefix, domainNumber, spriteID,
  165.        startCyl, endCyl); 
  166. }    
  167.  
  168. /*
  169.  *----------------------------------------------------------------------
  170.  *
  171.  * PrintLfsInfo --
  172.  *
  173.  *      Prints the domain prefix, the domain number,
  174.  *      the SpriteID, the start cylinder and size, and the index of
  175.  *      the LFS partition found on the stream
  176.  *
  177.  * Results:
  178.  *      None.
  179.  *
  180.  * Side effects:
  181.  *    Prints to stdout.
  182.  *
  183.  *----------------------------------------------------------------------
  184.  */
  185. int
  186. PrintLfsInfo(stream, deviceName, labelPtr)
  187.     int stream;
  188.     char *deviceName;
  189.     Disk_Label *labelPtr;
  190. {
  191.     LfsCheckPointHdr *headerPtr;
  192.     int index;
  193.     int area = 0;
  194.  
  195.     headerPtr = Disk_ReadLfsCheckPointHdr(stream, labelPtr, &area);
  196.     if (headerPtr == NULL) {
  197.     return FAILURE;
  198.     } 
  199.     /*
  200.      * only print header if subsequent information is going to be printed
  201.      */
  202.     index = PartitionIndex(deviceName, labelPtr);
  203.     if (index == -1) {
  204.     if (verbose == TRUE) {
  205.         if (headerNotPrinted == TRUE) {
  206.         PrintHeader();
  207.         headerNotPrinted = FALSE;
  208.         }
  209.         printf("%s: Bad partition.  ", deviceName);
  210.         printf("If this is the raw disk handle,\n");
  211.         printf("try specifying the `a' partition instead.\n");
  212.     }
  213.     free(headerPtr);
  214.     return FAILURE;
  215.     }
  216.     if (headerNotPrinted == TRUE) {
  217.     PrintHeader();
  218.     headerNotPrinted = FALSE;
  219.     }
  220.     PrintInfo(deviceName, LFS_NAME, headerPtr->domainPrefix, 
  221.           headerPtr->domainNumber, headerPtr->serverID, 
  222.           labelPtr, index);
  223.     free(headerPtr);
  224.     return SUCCESS;
  225. }
  226.  
  227. /*
  228.  *----------------------------------------------------------------------
  229.  *
  230.  * PrintOfsInfo --
  231.  *
  232.  *      Prints the domain prefix, the domain number,
  233.  *      the SpriteID, the start cylinder and size, and the index of
  234.  *      the OFS partition found on the stream.
  235.  *
  236.  * Results:
  237.  *      None.
  238.  *
  239.  * Side effects:
  240.  *    Prints to stdout.
  241.  *
  242.  *----------------------------------------------------------------------
  243.  */
  244. int
  245. PrintOfsInfo(stream, deviceName, labelPtr)
  246.     int stream;
  247.     char *deviceName;
  248.     Disk_Label *labelPtr;
  249. {
  250.     Ofs_DomainHeader *headerPtr;
  251.     Ofs_SummaryInfo *summaryPtr;
  252.     int index;
  253.  
  254.     headerPtr = Disk_ReadDomainHeader(stream, labelPtr);
  255.     if (headerPtr == NULL) {
  256.     return FAILURE;
  257.     }
  258.     summaryPtr = Disk_ReadSummaryInfo(stream, labelPtr);
  259.     if (summaryPtr == NULL) {
  260.     free(headerPtr);
  261.     return FAILURE;
  262.     }
  263.     index = PartitionIndex(deviceName, labelPtr);
  264.     if (index == -1) {
  265.     if (verbose == TRUE) {
  266.         if (headerNotPrinted == TRUE) {
  267.         PrintHeader();
  268.         headerNotPrinted = FALSE;
  269.         }
  270.         printf("%s: Bad partition.  ", deviceName);
  271.         printf("If this is the raw disk handle,\n");
  272.         printf("try specifying the `a' partition instead.\n");
  273.     }
  274.     free(headerPtr);
  275.     free(summaryPtr);
  276.     return FAILURE;
  277.     }
  278.     if (DISK_PARTITION(&(headerPtr->device)) != index) {
  279.     if (verbose == TRUE) {
  280.         if (headerNotPrinted == TRUE) {
  281.         PrintHeader();
  282.         headerNotPrinted = FALSE;
  283.         }
  284.         printf("%s: The partition has an OFS on it", deviceName);
  285.         printf(", but the OFS\nwas created on partition `%c'.\n",
  286.            DISK_PARTITION(&(headerPtr->device)) + 'a');
  287.     }
  288.     free(headerPtr);
  289.     free(summaryPtr);
  290.     return FAILURE;
  291.     }
  292.     if (headerNotPrinted == TRUE) {
  293.     PrintHeader();
  294.     headerNotPrinted = FALSE;
  295.     }
  296.     PrintInfo(deviceName, OFS_NAME, summaryPtr->domainPrefix, 
  297.           summaryPtr->domainNumber, headerPtr->device.serverID, 
  298.           labelPtr, index);
  299.     free(headerPtr);
  300.     free(summaryPtr);
  301.     return SUCCESS;
  302. }
  303.  
  304. /*
  305.  *----------------------------------------------------------------------
  306.  *
  307.  * main --
  308.  *
  309.  *      Open the device streams, then read and print the partition and 
  310.  *      file system info found to stdout.
  311.  *
  312.  * Results:
  313.  *      None.
  314.  *
  315.  * Side effects:
  316.  *    Prints to stdout.
  317.  *
  318.  *----------------------------------------------------------------------
  319.  */
  320. void
  321. main(argc, argv)
  322.     int argc;
  323.     char *argv[];
  324. {
  325.     int        stream;
  326.     int        n, fstype;
  327.     Disk_Label *labelPtr;
  328.     int        retval, problemEncountered = 0;
  329.  
  330.     argc = Opt_Parse(argc, argv, optionArray, numOptions);
  331.     if (argc == 1) {
  332.     Opt_PrintUsage(argv[0], optionArray, numOptions);
  333.     exit(FAILURE);
  334.     }
  335.     headerNotPrinted = TRUE;
  336.     for (n = 1; n < argc; n++) {
  337.     stream = open(argv[n], O_RDONLY, 0);
  338.     if (stream < 0) {
  339.         continue;
  340.     }
  341.     labelPtr = Disk_ReadLabel(stream);
  342.     if (labelPtr == NULL) {
  343.         if (verbose == TRUE) {
  344.         if (headerNotPrinted) {
  345.             PrintHeader();
  346.             headerNotPrinted = FALSE;
  347.         }
  348.         printf("fsinfo: cannot find label on device %s.  ", argv[n]);
  349.         printf("Is the label\non the disk of the correct type for ");
  350.         printf("the machine being used?\n");
  351.         }
  352.         problemEncountered = 1;
  353.         continue;
  354.     }
  355.     fstype = Disk_HasFilesystem(stream, labelPtr);
  356.     switch (fstype) {
  357.         case DISK_HAS_OFS:
  358.             retval = PrintOfsInfo(stream, argv[n], labelPtr);
  359.         if (retval == FAILURE) {
  360.             problemEncountered = 1;
  361.         }
  362.         break;
  363.         case DISK_HAS_LFS:
  364.             retval = PrintLfsInfo(stream, argv[n], labelPtr);
  365.         if (retval == FAILURE) {
  366.             problemEncountered = 1;
  367.         }
  368.         break;
  369.         default:
  370.             if (verbose == TRUE) {
  371.             if (headerNotPrinted) {
  372.             PrintHeader();
  373.             headerNotPrinted = FALSE;
  374.             }
  375.             printf("%s: no file system found\n", argv[n]);
  376.         }
  377.         break;
  378.     }
  379.     free(labelPtr);
  380.     }
  381.     if (headerNotPrinted == TRUE) {
  382.     printf("No file systems found.\n");
  383.     }
  384.     if (problemEncountered) {
  385.     exit(1);
  386.     } else {
  387.     exit(0);
  388.     }
  389. }
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.